﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

public class StringReader : BaseObject
{
    private static StringReader self;
    public StringReader() { }

    public static StringReader getInstance()
    {
        if (self == null)
        {
            self = new StringReader();
        }
        return self;
    }

    /// <summary>
    /// 读取文本到内存调用
    /// </summary>
    /// <param name="path">文本路径</param>
    public void LoadString(string path)
    {
        LoadString(path, Encoding.UTF8);
    }
    public void LoadString(string path, Encoding type)
    {
        StreamReader sr = new StreamReader(path, type);

        string mainStr = sr.ReadToEnd();

        string[] splitStr = mainStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

        foreach (string val in splitStr)
        {
            string[] spStr = val.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
            HandleProperties((spStr[0] as string).Trim(), (spStr[1] as string).Trim());
        }
    }

    /// <summary>
    /// 直接获取对应的字符串
    /// </summary>
    public string getSwfLangStr(string StrID)
    {
        if (CheckProperties(StrID) == false) return StrID;

        return (string)GetProperties(StrID);
    }

    /// <summary>
    /// 将指定的String中的每个格式项替换为相应对象的值的文本等效项xxx{@0:xxx}xxx
    /// </summary>
    /// <param name="StrID">字符key</param>
    /// <param name="valArr">参数对象数组</param>
    public string getSwfLangStrVar(string StrID, params object[] valArr)
    {
        if (CheckProperties(StrID) == false) return StrID;

        string strData = (string)GetProperties(StrID);

        int indexpre;
        int indexback;
        string strget;

        indexpre = strData.IndexOf("{");
        indexback = strData.IndexOf("}");

        //下一次搜索的起始偏移量,防止{@}嵌套时,造成死循环
        int nextOffset = 0;

        int firstIndex;

        int strFlagPre;
        int strFlagBack;
        string strFlag;

        while (indexpre != -1 && indexback != -1)
        {
            strget = strData.Substring(indexpre, indexback - indexpre + 1);

            firstIndex = strData.IndexOf("@", nextOffset);
            string strNum = strData.Substring(firstIndex + 1, strData.IndexOf(":", firstIndex) - firstIndex -1);
            int number = strNum==""?0:int.Parse(strNum);

            //处理填充字符串参数（如果有）
            strFlagPre = strData.IndexOf("!#[", nextOffset) + 3;

            if (strFlagPre > 2)
            {
                //前缀{!#[PeerageRank_]@0:}
                strFlagBack = strData.IndexOf("]@", nextOffset);
                strFlag = strData.Substring(strFlagPre, strFlagBack - strFlagPre + 1);
                valArr[number] = getSwfLangStr(strFlag + valArr[number]);
            }

            string strreplace = (String)valArr[number];

            strData = strData.Replace(strget, strreplace);

            nextOffset = indexpre + strreplace.Length;

            indexpre = strData.IndexOf("{", nextOffset);
            indexback = strData.IndexOf("}", nextOffset);
        }

        return strData;
    }

    /// <summary>
    /// xxx{0}xxx
    /// </summary>
    public string getSwfLangStrVarEx(string StrID, params object[] args)
    {
        if (CheckProperties(StrID) == false) return StrID;

        string strData = (string)GetProperties(StrID);

        return String.Format(strData, args);
    }




}